package com.hzlh.configuration;


import com.hzlh.utils.LhPropertiesUtils;
import com.hzlh.utils.PropertiesUtil;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
 * @Description: Mq配置
 * @Author: linker_zhang
 * @Date: Created in 2020/6/2 14:30
 * @Version:1.0.0
 */
@Configuration
public class RabbitConfig {
    private String host = LhPropertiesUtils.getProValue("spring.rabbitmq.host");
    private int port = Integer.valueOf(LhPropertiesUtils.getProValue("spring.rabbitmq.port"));
    private String username = LhPropertiesUtils.getProValue("spring.rabbitmq.username");
    private String password = LhPropertiesUtils.getProValue("spring.rabbitmq.password");

    //队列定义
    public static final String QUEUE_A = "XXX";

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host,port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost("/");
        connectionFactory.setPublisherConfirms(true);
        return connectionFactory;
    }

   @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    //必须是prototype类型
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate template = new RabbitTemplate(connectionFactory());
        return template;
    }

    /**
     * 获取队列A
     * @return
     */
    @Bean
    public Queue queueA() {
        return new Queue(QUEUE_A, true); //队列持久
    }
}